﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment
{
    class TodoListConsole
    {

        static TodoListBO tlbo = new TodoListBO();


        public static short Menu()
        {
            short error = 0;
            short enter = 3;

            do
            {
                Console.WriteLine("+-----------------------------------------------------------------------------+");
                Console.WriteLine("| {0,-76}|", "MENU");
                Console.WriteLine("+-----------------------------------------------------------------------------+");
                Console.WriteLine("| {0,-76}|", "1.Add Todo");
                Console.WriteLine("| {0,-76}|", "2.Show Todo List by Priority");
                Console.WriteLine("| {0,-76}|", "3.Show Todo List by Create Date");
                Console.WriteLine("| {0,-76}|", "4.Show Todo List by Due Date");
                Console.WriteLine("| {0,-76}|", "5.Show Todo List by Complete(Todo)");
                Console.WriteLine("| {0,-76}|", "6.Exit");
                Console.WriteLine("+-----------------------------------------------------------------------------+");
                Console.Write("| You Choice: ");
                try
                {
                    enter = Convert.ToInt16(Console.ReadLine());
                    if (enter > 6 || enter < 1)
                    {
                        throw new Exception();
                    }
                    Console.WriteLine("+-----------------------------------------------------------------------------+");
                }
                catch (Exception)
                {
                    Console.WriteLine("Nhap vao so tu 1 - 6");
                    error = 1;
                }
            }
            while (error == 1);
            return enter;
        }
        private static void Add()
        {

            Console.Clear();
            short error = 0;
            string content;
            DateTime create = DateTime.Now;
            DateTime due = new DateTime();
            int priority = 0;
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            Console.WriteLine("| {0,-76}|", "Add Todo");
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            Console.Write("| * Content: ");
            content = Console.ReadLine();
            do
            {
                Console.Write("| * Priority: ");
                try
                {
                    priority = Convert.ToInt32(Console.ReadLine());
                    error = 0;
                    if (priority > 5 || priority < 1)
                        throw new Exception();
                }
                catch
                {
                    Console.WriteLine("| Nhap so tu 1 - 5");
                    error = 1;
                }
            }
            while (error == 1);

            do
            {
                Console.Write("| * Due Date: ");
                try
                {
                    string strDt = Console.ReadLine();
                    System.Globalization.DateTimeFormatInfo dtfm = new System.Globalization.DateTimeFormatInfo();
                    dtfm.ShortDatePattern = "dd-MM-yyyy";
                    due = Convert.ToDateTime(strDt, dtfm);
                    error = 0;
                    if (due < create)
                    {
                        throw new Exception();
                    }
                }
                catch
                {
                    Console.WriteLine("| {0,-76}|", "Nhap Dung dinh dang: dd-MM-yyyy va phai lon hon ngay hien tai");
                    error = 1;
                }

            }
            while (error == 1);
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            if (tlbo.Add(new Todo(priority, content, create, due)) == 1)
            {
                Console.WriteLine("| Add Todo Successful");
            }
            else
            {
                Console.WriteLine("| Todo Already Exist");
            }
            Console.Write("| Press Enter key to Continue..."); Console.ReadLine();
            Console.Clear();
            NewMenu();
        }
        public static void ShowByPriority()
        {
            Console.Clear();
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            Console.WriteLine("| {0,-76}|", "Show Todo List by Priority");
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            Console.WriteLine("| {0,-4}|{5,-8}|{1,-27}|{2,-11}|{3,-11}|{4,-10}|", "ID", "Content", "Create Date", "Due Date", "Status", "Priority");
            Console.WriteLine("+-----------------------------------------------------------------------------+");

            foreach (Todo td in tlbo.GetByPriority())
            {
                Console.WriteLine("| {0,-4}|{5,8}|{1,-27}|{2,-11:dd-MM-yyyy}|{3,-11:dd-MM-yyyy}|{4,-10:Complete;4;Incomplete}|", td.ID, td.Content, td.CreateDate, td.DueDate, td.Status.GetHashCode(), td.Priority);
                Console.WriteLine("+-----------------------------------------------------------------------------+");
            }
            int choice = SubMenu();

            switch (choice)
            {
                case 1:
                    Console.Clear();
                    Console.WriteLine("+-----------------------------------------------------------------------------+");
                    Console.WriteLine("| {0,-76}|", "Set Complete Todo");
                    Console.WriteLine("+-----------------------------------------------------------------------------+");

                    int error = 0; int id = 0;
                    do
                    {
                        Console.Write("| ID Todo: ");
                        try
                        {
                            id = Convert.ToInt32(Console.ReadLine());
                            error = 0;
                            if (id < 1)
                                throw new Exception();
                        }
                        catch
                        {
                            Console.WriteLine("| Nhap id la so >0");
                            error = 1;
                        }
                    }
                    while (error == 1);
                    Todo sid = tlbo.GetByID(id);
                    if (sid != null)
                    {
                        if (tlbo.Complete(sid))
                        {
                            Console.WriteLine("| Set Complete Successful");
                        }
                    }
                    else
                        Console.WriteLine("| Not Found ID");
                    Console.Write("| Press Enter key to Continue..."); Console.ReadLine();
                    Console.Clear();
                    ShowByPriority();
                    break;
                case 2:
                    Console.Clear();
                    Console.WriteLine("+-----------------------------------------------------------------------------+");
                    Console.WriteLine("| {0,-76}|", "Remove Todo");
                    Console.WriteLine("+-----------------------------------------------------------------------------+");

                    error = 0; id = 0;
                    do
                    {
                        Console.Write("| ID Todo: ");
                        try
                        {
                            id = Convert.ToInt32(Console.ReadLine());
                            error = 0;
                            if (id < 1)
                                throw new Exception();
                        }
                        catch
                        {
                            Console.WriteLine("| Nhap id la so >0");
                            error = 1;
                        }
                    }
                    while (error == 1);
                    sid = tlbo.GetByID(id);
                    if (sid != null)
                    {
                        if (tlbo.Remove(sid) == 1)
                        {
                            Console.WriteLine("| Remove Successful");
                        }
                    }
                    else
                        Console.WriteLine("| Not Found ID");
                    Console.Write("| Press Enter key to Continue..."); Console.ReadLine();
                    Console.Clear();
                    ShowByPriority();
                    break;
                case 3:
                    Console.Clear();
                    NewMenu();
                    break;
            }
        }
        public static void ShowByCreateDate()
        {
            Console.Clear();
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            Console.WriteLine("| {0,-76}|", "Show Todo List by Create Date");
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            Console.WriteLine("| {0,-4}|{5,-8}|{1,-27}|{2,-11}|{3,-11}|{4,-10}|", "ID", "Content", "Create Date", "Due Date", "Status", "Priority");
            Console.WriteLine("+-----------------------------------------------------------------------------+");

            foreach (Todo td in tlbo.GetByCreateDate())
            {
                Console.WriteLine("| {0,-4}|{5,8}|{1,-27}|{2,-11:dd-MM-yyyy}|{3,-11:dd-MM-yyyy}|{4,-10:Complete;4;Incomplete}|", td.ID, td.Content, td.CreateDate, td.DueDate, td.Status.GetHashCode(), td.Priority);
                Console.WriteLine("+-----------------------------------------------------------------------------+");
            }
            int choice = SubMenu();

            switch (choice)
            {
                case 1:
                    Console.Clear();
                    Console.WriteLine("+-----------------------------------------------------------------------------+");
                    Console.WriteLine("| {0,-76}|", "Set Complete Todo");
                    Console.WriteLine("+-----------------------------------------------------------------------------+");

                    int error = 0; int id = 0;
                    do
                    {
                        Console.Write("| ID Todo: ");
                        try
                        {
                            id = Convert.ToInt32(Console.ReadLine());
                            error = 0;
                            if (id < 1)
                                throw new Exception();
                        }
                        catch
                        {
                            Console.WriteLine("| Nhap id la so >0");
                            error = 1;
                        }
                    }
                    while (error == 1);
                    Todo sid = tlbo.GetByID(id);
                    if (sid != null)
                    {
                        if (tlbo.Complete(sid))
                        {
                            Console.WriteLine("| Set Complete Successful");
                        }
                    }
                    else
                        Console.WriteLine("| Not Found ID");
                    Console.Write("| Press Enter key to Continue..."); Console.ReadLine();
                    Console.Clear();
                    ShowByCreateDate();
                    break;
                case 2:
                    Console.Clear();
                    Console.WriteLine("+-----------------------------------------------------------------------------+");
                    Console.WriteLine("| {0,-76}|", "Remove Todo");
                    Console.WriteLine("+-----------------------------------------------------------------------------+");

                    error = 0; id = 0;
                    do
                    {
                        Console.Write("| ID Todo: ");
                        try
                        {
                            id = Convert.ToInt32(Console.ReadLine());
                            error = 0;
                            if (id < 1)
                                throw new Exception();
                        }
                        catch
                        {
                            Console.WriteLine("| Nhap id la so >0");
                            error = 1;
                        }
                    }
                    while (error == 1);
                    sid = tlbo.GetByID(id);
                    if (sid != null)
                    {
                        if (tlbo.Remove(sid) == 1)
                        {
                            Console.WriteLine("| Remove Successful");
                        }
                    }
                    else
                        Console.WriteLine("| Not Found ID");
                    Console.Write("| Press Enter key to Continue..."); Console.ReadLine();
                    Console.Clear();
                    ShowByCreateDate();
                    break;
                case 3:
                    Console.Clear();
                    NewMenu();
                    break;
            }
        }

        public static void ShowByDueDate()
        {
            Console.Clear();
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            Console.WriteLine("| {0,-76}|", "Show Todo List by Due Date");
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            Console.WriteLine("| {0,-4}|{5,-8}|{1,-27}|{2,-11}|{3,-11}|{4,-10}|", "ID", "Content", "Create Date", "Due Date", "Status", "Priority");
            Console.WriteLine("+-----------------------------------------------------------------------------+");

            foreach (Todo td in tlbo.GetByDueDate())
            {
                Console.WriteLine("| {0,-4}|{5,8}|{1,-27}|{2,-11:dd-MM-yyyy}|{3,-11:dd-MM-yyyy}|{4,-10:Complete;4;Incomplete}|", td.ID, td.Content, td.CreateDate, td.DueDate, td.Status.GetHashCode(), td.Priority);
                Console.WriteLine("+-----------------------------------------------------------------------------+");
            }
            int choice = SubMenu();

            switch (choice)
            {
                case 1:
                    Console.Clear();
                    Console.WriteLine("+-----------------------------------------------------------------------------+");
                    Console.WriteLine("| {0,-76}|", "Set Complete Todo");
                    Console.WriteLine("+-----------------------------------------------------------------------------+");

                    int error = 0; int id = 0;
                    do
                    {
                        Console.Write("| ID Todo: ");
                        try
                        {
                            id = Convert.ToInt32(Console.ReadLine());
                            error = 0;
                            if (id < 1)
                                throw new Exception();
                        }
                        catch
                        {
                            Console.WriteLine("| Nhap id la so >0");
                            error = 1;
                        }
                    }
                    while (error == 1);
                    Todo sid = tlbo.GetByID(id);
                    if (sid != null)
                    {
                        if (tlbo.Complete(sid))
                        {
                            Console.WriteLine("| Set Complete Successful");
                        }
                    }
                    else
                        Console.WriteLine("| Not Found ID");
                    Console.Write("| Press Enter key to Continue...");Console.ReadLine();
                    Console.Clear();
                    ShowByDueDate();
                    break;
                case 2:
                    Console.Clear();
                    Console.WriteLine("+-----------------------------------------------------------------------------+");
                    Console.WriteLine("| {0,-76}|", "Remove Todo");
                    Console.WriteLine("+-----------------------------------------------------------------------------+");

                    error = 0; id = 0;
                    do
                    {
                        Console.Write("| ID Todo: ");
                        try
                        {
                            id = Convert.ToInt32(Console.ReadLine());
                            error = 0;
                            if (id < 1)
                                throw new Exception();
                        }
                        catch
                        {
                            Console.WriteLine("| Nhap id la so >0");
                            error = 1;
                        }
                    }
                    while (error == 1);
                    sid = tlbo.GetByID(id);
                    if (sid != null)
                    {
                        if (tlbo.Remove(sid) == 1)
                        {
                            Console.WriteLine("| Remove Successful");
                        }
                    }
                    else
                        Console.WriteLine("| Not Found ID");
                    Console.Write("| Press Enter key to Continue...");Console.ReadLine();
                    Console.Clear();
                    ShowByDueDate();
                    break;
                case 3:
                    Console.Clear();
                    NewMenu();
                    break;
            }
        }

        public static void ShowByComplete()
        {
            Console.Clear();
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            Console.WriteLine("| {0,-76}|", "Show Todo List by Complete(Todo)");
            Console.WriteLine("+-----------------------------------------------------------------------------+");
            Console.WriteLine("| {0,-4}|{5,-8}|{1,-27}|{2,-11}|{3,-11}|{4,-10}|", "ID", "Content", "Create Date", "Due Date", "Status", "Priority");
            Console.WriteLine("+-----------------------------------------------------------------------------+");

            foreach (Todo td in tlbo.GetByComplete())
            {
                Console.WriteLine("| {0,-4}|{5,8}|{1,-27}|{2,-11:dd-MM-yyyy}|{3,-11:dd-MM-yyyy}|{4,-10:Complete;4;Incomplete}|", td.ID, td.Content, td.CreateDate, td.DueDate, td.Status.GetHashCode(), td.Priority);
                Console.WriteLine("+-----------------------------------------------------------------------------+");
            }
            int choice = SubMenu();

            switch (choice)
            {
                case 1:
                    Console.Clear();
                    Console.WriteLine("+-----------------------------------------------------------------------------+");
                    Console.WriteLine("| {0,-76}|", "Set Complete Todo");
                    Console.WriteLine("+-----------------------------------------------------------------------------+");

                    int error = 0; int id = 0;
                    do
                    {
                        Console.Write("| ID Todo: ");
                        try
                        {
                            id = Convert.ToInt32(Console.ReadLine());
                            error = 0;
                            if (id < 1)
                                throw new Exception();
                        }
                        catch
                        {
                            Console.WriteLine("| Nhap id la so >0");
                            error = 1;
                        }
                    }
                    while (error == 1);
                    Todo sid = tlbo.GetByID(id);
                    if (sid != null)
                    {
                        if (tlbo.Complete(sid))
                        {
                            Console.WriteLine("| Set Complete Successful");
                        }
                    }
                    else
                        Console.WriteLine("| Not Found ID");
                    Console.Write("| Press Enter key to Continue...");Console.ReadLine();
                    Console.Clear();
                    ShowByComplete();
                    break;
                case 2:
                    Console.Clear();
                    Console.WriteLine("+-----------------------------------------------------------------------------+");
                    Console.WriteLine("| {0,-76}|", "Remove Todo");
                    Console.WriteLine("+-----------------------------------------------------------------------------+");

                    error = 0; id = 0;
                    do
                    {
                        Console.Write("| ID Todo: ");
                        try
                        {
                            id = Convert.ToInt32(Console.ReadLine());
                            error = 0;
                            if (id < 1)
                                throw new Exception();
                        }
                        catch
                        {
                            Console.WriteLine("| Nhap id la so >0");
                            error = 1;
                        }
                    }
                    while (error == 1);
                    sid = tlbo.GetByID(id);
                    if (sid != null)
                    {
                        if (tlbo.Remove(sid) == 1)
                        {
                            Console.WriteLine("| Remove Successful");
                        }
                    }
                    else
                        Console.WriteLine("| Not Found ID");
                    Console.Write("| Press Enter key to Continue...");Console.ReadLine();
                    ShowByComplete();
                    NewMenu();
                    break;
                case 3:
                    Console.Clear();
                    NewMenu();
                    break;
            }
        }
        static void NewMenu()
        {
            int enter = Menu();
            switch (enter)
            {
                case 1:
                    Add();
                    break;
                case 2:
                    ShowByPriority();
                    break;
                case 3:
                    ShowByCreateDate();
                    break;
                case 4:
                    ShowByDueDate();
                    break;
                case 5:
                    ShowByComplete();
                    break;
                case 6:
                    Environment.Exit(0);
                    break;
            }
        }
        static int SubMenu()
        {

            short error = 0;
            short enter = 3;

            do
            {
                Console.WriteLine();
                Console.WriteLine("+-----------------------------------------------------------------------------+");
                Console.WriteLine("| {0,-76}|", "SUB MENU");
                Console.WriteLine("+-----------------------------------------------------------------------------+");
                Console.WriteLine("| {0,-76}|", "1.Set Complete Todo");
                Console.WriteLine("| {0,-76}|", "2.Remove Todo");
                Console.WriteLine("| {0,-76}|", "3.Back to Main Menu");
                Console.WriteLine("+-----------------------------------------------------------------------------+");
                Console.Write("| You Choice: ");
                try
                {
                    enter = Convert.ToInt16(Console.ReadLine());
                    if (enter > 3 || enter < 1)
                    {
                        throw new Exception();
                    }
                    Console.WriteLine("+-----------------------------------------------------------------------------+");
                }
                catch (Exception)
                {
                    Console.WriteLine("| {0,-76}|", "Nhap vao so 1, 2 hoac 3");
                    error = 1;
                }
            }
            while (error == 1);
            return enter;
        }

        static void Main(string[] args)
        {

            NewMenu();
        }
    }
}
